package com.lmqb.controller;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lmqb.bean.LmCredit;
import com.lmqb.bean.LmCreditPromote;
import com.lmqb.bean.SysUser;
import com.lmqb.entry.MenuTitle;
import com.lmqb.entry.UBaseInfo;
import com.lmqb.page.AccountPager;
import com.lmqb.page.CreditPager;
import com.lmqb.page.CreditPromotePager;
import com.lmqb.page.LatefeePager;
import com.lmqb.page.LoanPager;
import com.lmqb.page.RepayPager;
import com.lmqb.service.CreditService;
import com.lmqb.service.JiaoYanService;
import com.lmqb.service.UserService;

@Controller
public class CreditController extends BaseController{

	@Autowired
	private JiaoYanService jiaoYanService;
	
	@Autowired
	private CreditService creditService;
	
	@Autowired
	private UserService userService;
	
	//借款人信用额度提升申请表（提额申请）
	@RequestMapping(value="creditPromote.html",method=RequestMethod.GET)
	public String toCreditPromote(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,CreditPromotePager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(CreditPromotePager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			CreditPromotePager userBankPager=creditService.selectAccountPager(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, userBankPager);
			response.setContentType("text/html;charset=UTF-8");
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("额度提升");
			menuTitle.setMenu("提额申请");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "creditPromoteList";
	}
	
	//提额通过页面
	@RequestMapping(value="toCreditPromote.html",method=RequestMethod.GET)
	public String toCreditPromote(HttpServletRequest request){
		System.out.println("提额申请通过"+request.getParameter("promoteId"));
		Integer promoteId=Integer.parseInt(request.getParameter("promoteId"));
		LmCreditPromote lmCreditPromote = creditService.selectByPrimaryKey(promoteId);
		System.out.println("############"+lmCreditPromote);
		request.setAttribute("lmCreditPromote", lmCreditPromote);
			return "isPassCredit";
		}
	
	//提额通过修改备注
	@RequestMapping(value="addIsPassCredit",method=RequestMethod.GET)
	public String addIsPassCredit(HttpServletRequest request,String remark,String promoteCredit) throws UnsupportedEncodingException{
		String promoteId = request.getParameter("promoteId");
		String userId = request.getParameter("userId");
		//new String(request.getParameter("remark").getBytes("ISO-8859-1"),"UTF-8")
		String remark1=new String(remark.getBytes("ISO-8859-1"),"UTF-8");
		System.out.println(promoteId+"-->"+userId+"-->"+remark1+"-->"+promoteCredit);
		//修改提额申请表
		LmCreditPromote lmCreditPromote = new LmCreditPromote();
		lmCreditPromote.setPromoteId(Integer.parseInt(promoteId));
		lmCreditPromote.setCheckRemark(remark1);
		lmCreditPromote.setPromoteStatus((byte)2);
		creditService.updateNoPassCreiet(lmCreditPromote);
		//修改提额用户表
		//UBaseInfo baseInfo=userService.selectBaseInfo(Integer.parseInt(userId));
		LmCredit lmCredit=new LmCredit();
		lmCredit.setUserId(Integer.parseInt(userId));
		BigDecimal promoteCredit1=new BigDecimal(promoteCredit);
		System.out.println("总金额"+promoteCredit1);
		lmCredit.setTotalCredit(promoteCredit1);
		creditService.updateIsPassCrdiet(lmCredit);
			return "redirect:creditPromote.html";
		}
	
	//提额不通过,调到修改备注页面
	@RequestMapping(value="noPassCreditPromote",method=RequestMethod.GET)
	public String noPassCreditPromote(HttpServletRequest request){
		Integer promoteId=Integer.parseInt(request.getParameter("promoteId"));
		System.out.println("提额申请不通过");
		request.setAttribute("promoteId", promoteId);
			return "noPassCredit";
		}
	
	//提额不通过,修改备注
	@RequestMapping(value="addNoPassCredit",method=RequestMethod.GET)
	public String addNoPassCredit(HttpServletRequest request,String remark) throws UnsupportedEncodingException{
		String remark1 = new String(remark.getBytes("ISO-8859-1"),"UTF-8");
		System.out.println(remark1);
	Integer promoteId=Integer.parseInt(request.getParameter("promoteId"));
	LmCreditPromote lmCreditPromote=new LmCreditPromote();
	lmCreditPromote.setPromoteId(promoteId);
	lmCreditPromote.setCheckRemark(remark1);
	lmCreditPromote.setPromoteStatus((byte)3);
	creditService.updateNoPassCreiet(lmCreditPromote);
		return "redirect:creditPromote.html";
	}
	//借款人信用额度总览
	@RequestMapping(value="credit.html",method=RequestMethod.GET)
	public String toCredit(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,CreditPager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(CreditPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			CreditPager userBankPager=creditService.selectCreditPager(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, userBankPager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("额度提升");
			menuTitle.setMenu("提额用户");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "creditList";
	}
	
	//滞纳金明细
	@RequestMapping(value="latefee.html",method=RequestMethod.GET)
	public String toLatefee(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,LatefeePager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(LatefeePager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			LatefeePager userBankPager=creditService.selectLatefeePager(pagerModel);
			request.setAttribute(pager, userBankPager);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "latefeeList";
	}
	
	//还款明细
	@RequestMapping(value="repay.html",method=RequestMethod.GET)
	public String toRepay(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,RepayPager pagerModel){
		
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(RepayPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			RepayPager userBankPager=creditService.selectRepayPager(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, userBankPager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("还款管理");
			menuTitle.setMenu("还款列表");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "repayList";
	}
}
